package com.nondo.dean.saprfc.common;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * 查找U8数据库信息
 */
public class U8AccountBean {
	/**
	 * 账号ID
	 */
	private String caccid = "";
	/**
	 * 年份
	 */
	private String iyear = "";
	/**
	 * 客户名称
	 */
	private String crmcode = "";
	/**
	 * 数据库名
	 */
	private String dbname = "";

	/**
	 * 根据总代编码和年份获取数据库信息
	 * @param caccid
	 * @param iyear
	 */
	public U8AccountBean(String caccid,String iyear) {
		String v_sql="";
		v_sql="select t1.cAcc_Id,t1.cDatabase,t2.cUnitAbbre from UA_AccountDatabase t1,UA_Account t2 where t1.cAcc_Id=t2.cAcc_Id and t1.cAcc_Id='"+caccid+"' and t1.iBeginYear<='"+iyear+"' and isnull(t1.iEndYear,'9999')>='"+iyear+"' and t1.cacc_id in('002','009')";
		ResultSet rs=DBUtil.executeQuery(v_sql);
		try {
			rs.next();
			String v_cacc_id=Util.null2String(rs.getString("cAcc_Id"));
			String v_crmcode=Util.null2String(rs.getString("cUnitAbbre")).trim();
			String v_dbname=Util.null2String(rs.getString("cDatabase")).trim();
			this.setCaccid(v_cacc_id);
			this.setIyear(iyear);
			this.setCrmcode(v_crmcode);
			this.setDbname(v_dbname);
			DBUtil.close(rs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public U8AccountBean(String caccid, String iyear, String crmcode,String dbname) {
		this.caccid = caccid;
		this.iyear = iyear;
		this.crmcode = crmcode;
		this.dbname = dbname;
	}
	public static U8AccountBean getAccountBean(String zdCode, String iyear){
		String v_sql="";
		U8AccountBean bean=null;
		v_sql="select t1.cAcc_Id,t1.cDatabase,t2.cUnitAbbre from UA_AccountDatabase t1,UA_Account t2 where t1.cAcc_Id=t2.cAcc_Id and t2.cUnitAbbre='"+zdCode+"' and t1.iBeginYear<='"+iyear+"' and isnull(t1.iEndYear,'9999')>='"+iyear+"'";
		ResultSet rs=DBUtil.executeQuery(v_sql);
		try {
			rs.next();
			String v_cacc_id=Util.null2String(rs.getString("cAcc_Id"));
			String v_crmcode=Util.null2String(rs.getString("cUnitAbbre")).trim();
			String v_dbname=Util.null2String(rs.getString("cDatabase")).trim();
			bean=new U8AccountBean(v_cacc_id,iyear,v_crmcode,v_dbname);
			DBUtil.close(rs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return bean;
	}
	public static ArrayList<U8AccountBean> getAllAccountBean(){
		ArrayList<U8AccountBean> arraylist=new ArrayList<U8AccountBean>();
		String v_sql="";
		v_sql="select cacc_id,iyear=(select max(t2.iyear) from ufsystem..ua_period t2 where t1.cacc_id=t2.cacc_id ),cunitabbre from ufsystem..UA_Account t1 where t1.cunitabbre<>'' and t1.cacc_id in('002','009')  order by cacc_id";
		ResultSet rs=DBUtil.executeQuery(v_sql);
		try {
			while (rs.next()) {
				String v_cacc_id=Util.null2String(rs.getString("cacc_id"));
				String v_iyear=Util.null2String(rs.getString("iyear"));
				String v_crmcode=Util.null2String(rs.getString("cunitabbre")).trim();
				U8AccountBean bean=U8AccountBean.getAccountBean(v_crmcode, v_iyear);
				arraylist.add(bean);
			}
			DBUtil.close(rs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return arraylist;
	}
	public String getCaccid() {
		return caccid;
	}

	public void setCaccid(String caccid) {
		this.caccid = caccid;
	}

	public String getIyear() {
		return iyear;
	}

	public void setIyear(String iyear) {
		this.iyear = iyear;
	}

	public String getCrmcode() {
		return crmcode;
	}

	/**
	 * 1 经销商，2 电商,3直营店
	 * @return
	 */
	public String getCrmType() {
		String v_crmType="1";
		if("B250000".equals(this.crmcode)==true){
			v_crmType="2";
		}else if("A000013".equals(this.crmcode)==true){
			v_crmType="3";
		}
		return v_crmType;
	}
	public void setCrmcode(String crmcode) {
		this.crmcode = crmcode;
	}

	public String getDbname() {
		return dbname;
	}

	public void setDbname(String dbname) {
		this.dbname = dbname;
	}

}
